package com.java46.admin.order.dao;

import com.java46.admin.dessert.dao.AdminDessertDao;
import com.java46.admin.order.entity.AllOrders;
import com.java46.admin.order.entity.OrderInfoAndDessert;
import com.java46.admin.user.dao.UserDaoT;
import com.java46.admin.user.entity.UsersEntity;
import com.java46.front.dessert.entity.DessertEntity;
import com.java46.front.order.entity.OrderinfoEntity;
import com.java46.front.order.entity.OrdersEntity;
import com.java46.admin.base.util.HibernateUtils;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import java.util.ArrayList;
import java.util.List;

public class OrderDao {
    //查询所有的订单
    private AdminDessertDao adminDessertDao=new AdminDessertDao();
    private UserDaoT userDaoT=new UserDaoT();
    public List<AllOrders> getOrders(int offset,int pageSize,String condition){
        List<AllOrders> list=new ArrayList<>();
        Session session = HibernateUtils.getSession();
        try{
            Query query = session.createQuery("from com.java46.front.order.entity.OrdersEntity oe where oe.orderState=0 and  oe.orderId like "+" '%"+condition+"%' order by oe.time DESC");
            query.setFirstResult(offset); //设置分页页码
            query.setMaxResults(pageSize);
            List<OrdersEntity> ordersList = query.list();
            for(OrdersEntity oe:ordersList){
                AllOrders allOrders=new AllOrders();
                UsersEntity user= userDaoT.getUserByUserId(oe.getUserId());
                List<OrderInfoAndDessert> orderInfoList = this.getOrderInfoByOrderId(oe.getOrderId());
                allOrders.setUsers(user);
                allOrders.setOrders(oe);
                allOrders.setOrderInfos(orderInfoList);
                list.add(allOrders);
            }
            return list;
        }catch (Exception e){
          throw e;
        }finally {
            session.close();
        }
    }
    //查询总条数
    public int getOrdersCount(String condition){
        int count=0;
        Session session = HibernateUtils.getSession();
        try{
            String hql = "select COUNT(1) from com.java46.front.order.entity.OrdersEntity oe where oe.orderState=0 and  oe.orderId like "+" '%"+condition+"%'";
            Query query = session.createQuery(hql);
            count = ((Long) query.iterate().next()).intValue();
        }catch(RuntimeException re){
            throw re;
        }finally {
            session.close();
        }
        return count;
    }
    //根据订单详情表中订单表号查询
    public List<OrderInfoAndDessert> getOrderInfoByOrderId(int orderId){
        List<OrderInfoAndDessert> list=new ArrayList<>();
        Session session = HibernateUtils.getSession();
        try {
            Query query = session.createQuery("from com.java46.front.order.entity.OrderinfoEntity as f where f.orderId=?1");
            query.setParameter(1,orderId);
            List<OrderinfoEntity> orderinfoEntities = query.list();
            for(OrderinfoEntity of:orderinfoEntities){
                OrderInfoAndDessert orderInfoAndDessert=new OrderInfoAndDessert();
                DessertEntity dessert = adminDessertDao.getDessertByDessertId(of.getDessertId());
                orderInfoAndDessert.setDessert(dessert);
                orderInfoAndDessert.setOrderInfo(of);
                list.add(orderInfoAndDessert);
            }
            return list;
        }catch (Exception e){
            throw e;
        }finally {
            session.close();
        }
    }
    //删除订单
    public  void deleteOrders(int orderId){
        Session session = HibernateUtils.getSession(); //拿到session
        Transaction transaction = session.beginTransaction();
        Query query =session.createQuery(" update com.java46.front.order.entity.OrdersEntity as oe set oe.orderState=1 where oe.orderId=?1");
        query.setParameter(1,orderId);
        query.executeUpdate();
        transaction.commit();
        session.close();
    }
    //根据OrderID查询订单详情
    public AllOrders getOrdersByOrderId(int orderId){
        Session session = HibernateUtils.getSession();
        try{
            Query query = session.createQuery("from com.java46.front.order.entity.OrdersEntity oe where oe.orderId=?1");
            //赋值
            query.setParameter(1,orderId);
            //拿到查询出的结果
            OrdersEntity orders=(OrdersEntity)query.uniqueResult();
            //得到AllOrders实体的对象
            AllOrders allOrders=new AllOrders();
            //得到此订单的用户信息
            UsersEntity user= userDaoT.getUserByUserId(orders.getUserId());
            //得到此订单的商品信息
            List<OrderInfoAndDessert> orderInfoList = this.getOrderInfoByOrderId(orders.getOrderId());
            allOrders.setUsers(user);
            allOrders.setOrders(orders);
            allOrders.setOrderInfos(orderInfoList);
            return allOrders;
        }catch (Exception e){
            throw e;
        }finally {
            session.close();
        }
    }
}
